package com.kai.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.kai.constant.StatusConstant;
import com.kai.dto.ExpressOrderDTO;
import com.kai.dto.ExpressOrderQueryDTO;
import com.kai.entity.ExpressOrder;
import com.kai.mapper.ExpressOrderMapper;
import com.kai.result.PageResult;
import com.kai.service.ExpressService;
import com.kai.threadlocal.BaseContest;
import com.kai.vo.ExpressOrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class ExpressServiceImpl implements ExpressService {

    @Autowired
    private ExpressOrderMapper expressOrderMapper;

    @Override
    public ExpressOrderVO submitExpress(ExpressOrderDTO expressOrderDTO) {
        //属性值拷贝
        ExpressOrder expressOrder = new ExpressOrder();
        BeanUtils.copyProperties(expressOrderDTO, expressOrder);
        //业务处理
        //状态 待接单
        expressOrder.setExpressStatus(StatusConstant.TO_BE_CONFIRMED);
        //下单 用户id
        expressOrder.setUserId(BaseContest.getCurrentId());
        //逻辑删除
        expressOrder.setIsDeleted(0);
        //订单编号
        expressOrder.setOrderNumber(System.currentTimeMillis() + "");

        expressOrderMapper.addOrder(expressOrder);
        ExpressOrderVO expressOrderVO = new ExpressOrderVO();
        BeanUtils.copyProperties(expressOrder, expressOrderVO);

        return expressOrderVO;
    }

    @Override
    public PageResult getHistoryOrders(ExpressOrderQueryDTO expressOrderQueryDTO) {

        PageHelper.startPage(expressOrderQueryDTO.getPage(), expressOrderQueryDTO.getPageSize());
        //查询人
        expressOrderQueryDTO.setUserId(BaseContest.getCurrentId());

        Page<ExpressOrderVO> page = expressOrderMapper.getHistoryOrders(expressOrderQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public void cancelOrder(Long expressId) {
        ExpressOrder express = ExpressOrder.builder().id(expressId).build();
        expressOrderMapper.cancelOrder(express);
    }
}
